def find_set(x):
    if x != s[x]:
        s[x] = find_set(s[x])
    return s[x]

def merge_set(x, y):
    x, y = find_set(x), find_set(y)
    if x != y:
        s[x] = s[y]

m, n = map(int, input().split())
k = int(input())
s = list(range(m * n + 1))

for _ in range(k):
    a, b = map(int, input().split())
    merge_set(a, b)

for i in range(1, m * n + 1):
    find_set(i)

set_s = set(s)
print(len(set_s) - 1)